import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';    // 默认主题
import '../static/css/icon.css';
import App from './App';
import router from './router';
import store from './vuex/store'
import request from './assets/scripts/common/ajax';
import splRequest from './assets/scripts/common/splAjax';
import apiReq from './assets/scripts/common/apiReq';
import filters from './assets/scripts/common/filters';
import axios from 'axios';
import "babel-polyfill";

Vue.use(ElementUI, { size: 'small' });

Vue.prototype.$axios = axios;
Vue.prototype.request = request;
Vue.prototype.splRequest = splRequest;
Vue.prototype.apiReq = apiReq;
Object.keys(filters).forEach(key => Vue.filter(key, filters[key]))
Vue.prototype.formatFace = (facetxt) => {
    const regFace = /\[.+?\]/g
    const regEnter = /\r?\n/g
    facetxt = facetxt.replace(regFace, (key, val) => {
        return '<img class="emotion-img" src="' + store.state.faceLst[key] + '" />'
    })
    // .replace(regEnter, '<br />')
    return facetxt
}

Vue.prototype.checkReplyIndx = (index) => {
    return 'reply' + index
}
Vue.prototype.checkAudioIndx = (index) => {
    return 'audio' + index
}
//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
    const role = store.getters.getUsrInfo
    if (!role.uname && to.path !== '/login') {
        next('/login')
    } else if(to.path === '/login' && typeof (role.uname) !== 'undefined' && role.uname.length > 0) {
        // 如果已登录并且跳转页面为登录页面
        next('/dashboard')
    } else {
        next()
    }
})

window.wVue = new Vue({
    store,
    router,
    render: h => h(App)
}).$mount('#app');